//
//  NormalOrderInfo.m
//  Findif
//
//  Created by Mac on 2018/11/14.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "NormalOrderInfo.h"

@interface NormalOrderInfo ()

@property (nonatomic, weak)UIImageView * iconImageView;
@property (nonatomic, weak)UILabel * shopNameLabel;
@property (nonatomic, weak)UILabel * stateLabel;
@property (nonatomic, weak)UIImageView * goodsImageView;
@property (nonatomic, weak)UILabel * goodsNameLabel;
@property (nonatomic, strong)UILabel * priceLabel;
@property (nonatomic, strong)UILabel * timeLabel;
@property (nonatomic, strong)UILabel * countLabel;
@property (nonatomic, strong)UILabel * disLabel;
@property (nonatomic, strong)UILabel * tuanLabel;
@property (nonatomic, strong)UILabel * balanceLabel;
@property (nonatomic, strong)UILabel * tuanValueLabel;
@property (nonatomic, strong)UILabel * disValueLabel;
@property (nonatomic, strong)UILabel * balanceValueLabel;
@property (nonatomic, strong)UILabel * payLabel;
@property (nonatomic, weak)UILabel * payValueLabel;
@property (nonatomic, strong)UILabel * vailLabel;
@property (nonatomic, weak)UIButton * cancleButton;
@property (nonatomic, weak)UIButton * sureButton;

@property (nonatomic, strong)UIView * oneLineView;
@property (nonatomic, strong)UIView * twoLineView;
@property (nonatomic, strong)UIView * threeLineView;
@property (nonatomic, strong)UIView * fourLineView;
@property (nonatomic, strong)UIView * fiveLineView;

@property (nonatomic, weak)UIImageView * localImageView;
@property (nonatomic, strong)UILabel * localLabel;
@property (nonatomic, strong)UILabel * dateLabel;
@property (nonatomic, strong)UILabel * tuanTimeLabel;
@property (nonatomic, strong)UILabel * orPriceLabel;

@end

@implementation NormalOrderInfo

- (void)setDict:(NSDictionary *)dict{
    
    _dict = dict;
//    NSLog(@"dict===%@",dict);
    self.shopNameLabel.text = self.dict[@"merchants_name"];
    NSString * shopImg = [NSString stringWithFormat:@"%@%@",BASEURLSTR,dict[@"merchants_img"]];
    NSURL * shopUrl = [NSURL URLWithString:shopImg];
    [self.iconImageView sd_setImageWithURL:shopUrl];
    NSString * goodsImg = dict[@"merchants_goods_img"];
    NSArray * goodImgArray = [goodsImg componentsSeparatedByString:@","];
    NSString * goodsImgStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,goodImgArray.firstObject];
    NSURL * goodsUrl = [NSURL URLWithString:goodsImgStr];
    [self.goodsImageView sd_setImageWithURL:goodsUrl];
    self.goodsNameLabel.text = dict[@"merchants_goods_name"];
    self.priceLabel.text = [NSString stringWithFormat:@"￥%@",dict[@"merchatns_goods_platform_discount_price"]];
    NSString * orString = [NSString stringWithFormat:@"￥%@",dict[@"merchatns_goods_platform_price"]];
    //中划线
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:orString attributes:attribtDic];
    
    self.orPriceLabel.attributedText = attribtStr;
    
    self.localLabel.text = [NSString stringWithFormat:@"%@",self.dict[@"merchants_address"]];

    int merchants_goods_type = [dict[@"merchants_goods_type"] intValue];
    if (merchants_goods_type == 1) {
        //普通
        self.timeLabel.hidden = NO;
        self.countLabel.hidden = NO;
        self.dateLabel.hidden = YES;
        self.tuanTimeLabel.hidden = YES;
        self.timeLabel.text = [NSString stringWithFormat:@"有效时间:%@",dict[@"merchants_goods_available_time"]];
        self.countLabel.text = [NSString stringWithFormat:@"*%@",dict[@"goods_order_buy_number"]];
    }else{
        
        self.countLabel.hidden = YES;
        self.timeLabel.hidden = YES;
        self.dateLabel.hidden = NO;
        self.tuanTimeLabel.hidden = NO;
//        self.timeLabel.text = [NSString stringWithFormat:@"拼团场次:%@",dict[@"goods_order_goods_field"]];
        NSString * timeStr = [NSString stringWithFormat:@"%@",dict[@"goods_order_goods_field"]];
        NSArray *array = [timeStr componentsSeparatedByString:@" "];
        self.dateLabel.text = [NSString stringWithFormat:@"日期:%@",array.firstObject];
        self.tuanTimeLabel.text = [NSString stringWithFormat:@"时间:%@",array.lastObject];
    }
    
    float orPrice = [dict[@"merchatns_goods_platform_price"] floatValue];
    float price = [dict[@"merchatns_goods_platform_discount_price"] floatValue];
    float res = orPrice - price;
    
    self.disValueLabel.text = [NSString stringWithFormat:@"-￥%.2f",res];
    self.tuanValueLabel.text = [NSString stringWithFormat:@"-￥%@",dict[@"goods_order_user_coupons_money"]];
    self.balanceValueLabel.text = [NSString stringWithFormat:@"-￥%@",dict[@"goods_order_user_bill_money"]];
    self.payValueLabel.text = [NSString stringWithFormat:@"￥%@",dict[@"goods_order_money"]];
    int status = [self.dict[@"goods_order_status"] intValue];
    
    if ([self.from isEqualToString:@"shop"]) {
        
        self.cancleButton.hidden = YES;
        self.sureButton.hidden = YES;
        if (status == 0){
            
            self.stateLabel.text = @"待付款";
        }else if (status == 1){
            
            self.stateLabel.text = @"待使用";
        }else if (status == 3){
            
            self.stateLabel.text = @"待评价";
        }else if (status == 2){
            
            self.stateLabel.text = @"已取消";
        }else if (status == 4){
            
            self.stateLabel.text = @"已失效";
        }else if (status == 5){
            
            self.stateLabel.text = @"已评价";
        }
    }else{
        
        if (status == 0){
            
            self.stateLabel.text = @"待付款";
            [self.cancleButton setTitle:@"取消订单" forState:UIControlStateNormal];
            [self.sureButton setTitle:@"立即付款" forState:UIControlStateNormal];
        }else if (status == 1){
            
            self.stateLabel.text = @"待使用";
            [self.cancleButton setTitle:@"取消订单" forState:UIControlStateNormal];
            [self.sureButton setTitle:@"立即使用" forState:UIControlStateNormal];
        }else if (status == 3){
            
            self.stateLabel.text = @"待评价";
            [self.cancleButton setTitle:@"再来一单" forState:UIControlStateNormal];
            [self.sureButton setTitle:@"立即评价" forState:UIControlStateNormal];
        }else if (status == 2){
            
            self.stateLabel.text = @"已取消";
            [self.cancleButton setTitle:@"删除订单" forState:UIControlStateNormal];
            [self.sureButton setTitle:@"再来一单" forState:UIControlStateNormal];
        }else if (status == 4){
            
            self.stateLabel.text = @"已失效";
            [self.cancleButton setTitle:@"删除订单" forState:UIControlStateNormal];
            [self.sureButton setTitle:@"再来一单" forState:UIControlStateNormal];
        }else if (status == 5){
            
            self.stateLabel.text = @"已评价";
            [self.cancleButton setTitle:@"删除订单" forState:UIControlStateNormal];
            [self.sureButton setTitle:@"再来一单" forState:UIControlStateNormal];
        }
    }
    
}

- (instancetype)initWithFrame:(CGRect)frame{
    
    if ([super initWithFrame:frame]) {
        
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI{
    
    self.oneLineView = [self setupLineView];
    [self addSubview:self.oneLineView];
    self.twoLineView = [self setupLineView];
    [self addSubview:self.twoLineView];
    self.threeLineView = [self setupLineView];
    [self addSubview:self.threeLineView];
    self.fourLineView = [self setupLineView];
    [self addSubview:self.fourLineView];
    self.fiveLineView = [self setupLineView];
    [self addSubview:self.fiveLineView];
    
    UIImageView * iconImageView = [UIImageView new];
    
    iconImageView.backgroundColor = [UIColor lightGrayColor];
    //    [iconImageView sd_setImageWithURL:shopUrl];
    iconImageView.layer.cornerRadius = 15 * SCREENH6;
    iconImageView.layer.masksToBounds = YES;
    
    self.iconImageView = iconImageView;
    
    [self addSubview:self.iconImageView];
    
    
    UILabel * shopNameLabel = [UILabel new];
    
    shopNameLabel.textColor = [UIColor lightGrayColor];
    shopNameLabel.text = @"---";
    shopNameLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.shopNameLabel = shopNameLabel;
    
    [self addSubview:self.shopNameLabel];
    
    
    UILabel * goodsNameLabel = [UILabel new];
    
    goodsNameLabel.textColor = ZRBlackColor;
    goodsNameLabel.text = @"---";
    goodsNameLabel.font = [UIFont boldSystemFontOfSize:15 * SCREENH6];
    
    self.goodsNameLabel = goodsNameLabel;
    
    [self addSubview:self.goodsNameLabel];
    
    
    UIImageView * goodsImageView = [UIImageView new];
    
    goodsImageView.backgroundColor = [UIColor lightGrayColor];
    //    [goodsImageView sd_setImageWithURL:goodsUrl];
    
    self.goodsImageView = goodsImageView;
    
    [self addSubview:self.goodsImageView];
    
    UILabel * stateLabel = [UILabel new];
    
    stateLabel.textColor = ColorWithRGB(0x53d499);
    stateLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    stateLabel.text = @"----";
    
    self.stateLabel = stateLabel;
    
    [self addSubview:self.stateLabel];
    
    self.timeLabel = [self setupLabelWithTitle:@"有效时间：2018-10-10" WithType:@"gray"];
    self.timeLabel.numberOfLines = 0;
    self.timeLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    self.timeLabel.textColor = ZRBlackColor;
    [self addSubview:self.timeLabel];
    self.priceLabel = [self setupLabelWithTitle:@"￥19" WithType:@"black"];
    self.priceLabel.textAlignment = NSTextAlignmentRight;
    [self addSubview:self.priceLabel];
    self.countLabel = [self setupLabelWithTitle:@"*1" WithType:@"gray"];
    [self addSubview:self.countLabel];
    self.disLabel = [self setupLabelWithTitle:@"商品折扣" WithType:@"black"];
    [self addSubview:self.disLabel];
    self.tuanLabel = [self setupLabelWithTitle:@"优惠券抵扣" WithType:@"black"];
    [self addSubview:self.tuanLabel];
    self.balanceLabel = [self setupLabelWithTitle:@"余额抵扣" WithType:@"black"];
    [self addSubview:self.balanceLabel];
    
    self.orPriceLabel = [self setupLabelWithTitle:@"￥23" WithType:@"gray"];
    self.orPriceLabel.textAlignment = NSTextAlignmentRight;
    [self addSubview:self.orPriceLabel];

    self.tuanValueLabel = [self setupYelloowLabelWith:@"-￥5"];
    [self addSubview:self.tuanValueLabel];
    self.disValueLabel = [self setupYelloowLabelWith:@"-￥5"];
    [self addSubview:self.disValueLabel];
    self.balanceValueLabel = [self setupYelloowLabelWith:@"-￥5"];
    [self addSubview:self.balanceValueLabel];
    
    self.payLabel = [self setupLabelWithTitle:@"实付" WithType:@"black"];
    [self addSubview:self.payLabel];
    
    UILabel * payValueLabel = [UILabel new];
    
    payValueLabel.text = @"￥15";
    payValueLabel.textColor = ColorWithRGB(0xfac917);
    payValueLabel.font = [UIFont systemFontOfSize:12 * SCREENH6];
    
    self.payValueLabel = payValueLabel;
    [self addSubview:self.payValueLabel];
    
    UIImageView * localImageView = [UIImageView new];
    
    localImageView.image = [UIImage imageNamed:@"place(1)"];
    
    self.localImageView = localImageView;
    [self addSubview:self.localImageView];
    
    self.dateLabel = [self setupLabelWithTitle:@"日期:" WithType:@"black"];
    [self addSubview:self.dateLabel];
    self.tuanTimeLabel = [self setupLabelWithTitle:@"时间:" WithType:@"black"];
    [self addSubview:self.tuanTimeLabel];
    self.localLabel = [self setupLabelWithTitle:@"地址" WithType:@"black"];
    self.localLabel.textColor = ColorWithRGB(0x4c87d8);
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    self.localLabel.userInteractionEnabled = YES;
    [self.localLabel addGestureRecognizer:tapGR];
    
    [self addSubview:self.localLabel];
    
    self.vailLabel = [self setupLabelWithTitle:@"订单有效时间:22222222" WithType:@"gray"];
    self.vailLabel.hidden = YES;
    [self addSubview:self.vailLabel];
    
    
    UIButton * cancleButton = [UIButton new];
    
    [cancleButton setTitle:@"取消订单" forState:UIControlStateNormal];
    [cancleButton setTitleColor:ZRBlackColor forState:UIControlStateNormal];
    [cancleButton setBackgroundColor:ZRWhiteColor];
    cancleButton.layer.cornerRadius = 5;
    cancleButton.layer.masksToBounds = YES;
    cancleButton.layer.borderColor = [ZRBlackColor CGColor];
    cancleButton.layer.borderWidth = 1 * SCREENH6;
    cancleButton.titleLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    [cancleButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    
    self.cancleButton = cancleButton;
    
    [self addSubview:self.cancleButton];
    
    
    UIButton * sureButton = [UIButton new];
    
    [sureButton setTitle:@"立即付款" forState:UIControlStateNormal];
    [sureButton setTitleColor:ZRBlackColor forState:UIControlStateNormal];
    [sureButton setBackgroundColor:ColorWithRGB(0xFFE055)];
    sureButton.layer.cornerRadius = 5;
    sureButton.layer.masksToBounds = YES;
    sureButton.titleLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    [sureButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    
    self.sureButton = sureButton;
    
    [self addSubview:self.sureButton];
}


- (void)layoutSubviews{
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self).offset(10 * SCREENH6);
        make.top.mas_equalTo(self).offset(15 * SCREENH6);
        make.height.width.mas_equalTo(30 * SCREENH6);
    }];
    
    [self.shopNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.iconImageView.mas_trailing).offset(5 * SCREENH6);
        make.centerY.mas_equalTo(self.iconImageView);
    }];
    
    [self.stateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self).offset(-10 * SCREENH6);
        make.centerY.mas_equalTo(self.shopNameLabel);
    }];
    
    [self.oneLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.iconImageView);
        make.trailing.mas_equalTo(self).offset(-10 * SCREENH6);
        make.height.mas_equalTo(1 * SCREENH6);
        make.top.mas_equalTo(54 * SCREENH6);
    }];
    
    [self.goodsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(85 * SCREENH6);
        make.top.mas_equalTo(self.oneLineView.mas_bottom).offset(10 * SCREENH6);
        make.leading.mas_equalTo(self.oneLineView);
    }];
    
    [self.goodsNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.oneLineView.mas_bottom).offset(13 * SCREENH6);
        make.leading.mas_equalTo(self.goodsImageView.mas_trailing).offset(5 * SCREENH6);
    }];
    
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.oneLineView);
        make.centerY.mas_equalTo(self.goodsNameLabel);
    }];
    
    [self.orPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.priceLabel);
        make.top.mas_equalTo(self.priceLabel.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.goodsNameLabel);
        make.trailing.mas_equalTo(self.priceLabel.mas_leading).offset(-5 * SCREENH6);
        make.top.mas_equalTo(self.goodsNameLabel.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.goodsNameLabel);
        make.top.mas_equalTo(self.goodsNameLabel.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.tuanTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.dateLabel);
        make.top.mas_equalTo(self.dateLabel.mas_bottom).offset(5 * SCREENH6);
    }];
    
//    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.trailing.mas_equalTo(self.priceLabel);
//        make.centerY.mas_equalTo(self.timeLabel);
//    }];
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.timeLabel);
        make.top.mas_equalTo(self.timeLabel.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.localImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.goodsImageView);
        make.top.mas_equalTo(self.goodsImageView.mas_bottom).offset(10 * SCREENH6);
        make.width.mas_equalTo(13 * SCREENH6);
        make.height.mas_equalTo(16 * SCREENH6);
    }];
    
    [self.localLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.localImageView.mas_trailing).offset(5 * SCREENH6);
        make.centerY.mas_equalTo(self.localImageView);
    }];
    
    [self.disLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.goodsImageView);
        make.top.mas_equalTo(self.localImageView.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.disValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.priceLabel);
        make.centerY.mas_equalTo(self.disLabel);
    }];
    
    [self.twoLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.oneLineView);
        make.top.mas_equalTo(self.disLabel.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.tuanLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.disLabel);
        make.top.mas_equalTo(self.twoLineView.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.tuanValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.disValueLabel);
        make.centerY.mas_equalTo(self.tuanLabel);
    }];
    
    [self.threeLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.twoLineView);
        make.top.mas_equalTo(self.tuanLabel.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.balanceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.tuanLabel);
        make.top.mas_equalTo(self.threeLineView.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.balanceValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.tuanValueLabel);
        make.centerY.mas_equalTo(self.balanceLabel);
    }];
    
    [self.fiveLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.threeLineView);
        make.top.mas_equalTo(self.balanceLabel.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.payValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.tuanValueLabel);
        make.top.mas_equalTo(self.fiveLineView.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.payLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.payValueLabel.mas_leading);
        make.centerY.mas_equalTo(self.payValueLabel);
    }];
    
    [self.fourLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.fiveLineView);
        make.top.mas_equalTo(self.payValueLabel.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.fourLineView.mas_bottom).offset(10 * SCREENH6);
        make.trailing.mas_equalTo(self.fourLineView);
        make.width.mas_equalTo(70 * SCREENH6);
        make.height.mas_equalTo(25 * SCREENH6);
    }];
    
    [self.cancleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.height.width.mas_equalTo(self.sureButton);
        make.trailing.mas_equalTo(self.sureButton.mas_leading).offset(-10 * SCREENH6);
    }];
    
    [self.vailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.goodsImageView);
        make.centerY.mas_equalTo(self.cancleButton);
    }];
}

- (void)clickButton:(UIButton *)button{
    
    if (self.normalOrderInfoDelegate && [self.normalOrderInfoDelegate respondsToSelector:@selector(clickItemButton:)])
    {
        [self.normalOrderInfoDelegate clickItemButton:button.titleLabel.text];
    }
}

- (void)tapAction{
    
    if (self.normalOrderInfoDelegate && [self.normalOrderInfoDelegate respondsToSelector:@selector(clickItemButton:)])
    {
        [self.normalOrderInfoDelegate clickItemButton:@"local"];
    }
}

- (UILabel *)setupLabelWithTitle:(NSString *)title WithType:(NSString *)type{
    
    UILabel * label = [UILabel new];
    
    label.text = title;
    if ([type isEqualToString:@"black"]) {
        
        label.textColor = ZRBlackColor;
        label.font = [UIFont systemFontOfSize:14 * SCREENH6];
    }else{
        
        label.textColor = [UIColor lightGrayColor];
        label.font = [UIFont systemFontOfSize:13 * SCREENH6];
    }
    
    
    return label;
}

- (UILabel *)setupYelloowLabelWith:(NSString *)title{
    
    UILabel * label = [UILabel new];
    
    label.text = title;
    label.textColor = ColorWithRGB(0xfac917);
    label.font = [UIFont systemFontOfSize:13 * SCREENH6];
    
    return label;
}

- (UIView *)setupLineView{
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xF5F5F5);
    
    return lineView;
}



@end
